package net.synck.gcontacts.wizards;

import net.synck.gcontacts.views.ViewContentProvider;
import net.synck.gcontacts.views.ViewLabelProvider;


import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.google.gdata.data.contacts.ContactEntry;

public class MergeWizardPageOne extends WizardPage {

	private Composite container;
	private TableViewer viewer;
	//private Object[] contacts; 

	private ContactEntry primaryContact;
			
	ContactEntry getPrimaryContact() {
		return primaryContact;
	}

	protected MergeWizardPageOne() {
		super("First Page");
		setTitle("Merge contacts");
		setDescription("Select primary contact");

	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		createTableViewer(container);
		layout.numColumns = 1;

		viewer.setInput(((MergeWizard)getWizard()).getContacts());
		
		setControl(container);
		setPageComplete(false);
	}

	private void createTableViewer(Composite parent) {
		viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setContentProvider(new IStructuredContentProvider() {

			@Override
			public void dispose() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
				// TODO Auto-generated method stub				
			}

			@Override
			public Object[] getElements(Object inputElement) {
				if (inputElement instanceof IStructuredSelection) {
					return ((IStructuredSelection)inputElement).toArray();
				}
				return null;
			}
			
		});
		viewer.setLabelProvider(new ViewLabelProvider());
				
		viewer.setComparator( new ViewerComparator() {
			@Override
			public int compare(Viewer viewer, Object e1, Object e2) {
				if (e1 instanceof ContactEntry && e2 instanceof ContactEntry) {
					return ((ContactEntry)e1).getTitle().getPlainText().compareTo(					
							((ContactEntry)e2).getTitle().getPlainText()
					);
				}
				return super.compare(viewer, e1, e2);
			}
			
		});
		
		
		Table table = viewer.getTable();
		
				
		TableColumn column = new TableColumn(table, SWT.LEFT,0);		
		column.setText("Name");
		column.setWidth(200);
		table.setSortColumn(column);
		
		column = new TableColumn(table, SWT.LEFT,1);		
		column.setText("Email");
		column.setWidth(150);
		
		column = new TableColumn(table, SWT.LEFT,2);		
		column.setText("Phone");
		column.setWidth(150);
		
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				if (((IStructuredSelection)event.getSelection()).getFirstElement() instanceof ContactEntry)
					primaryContact = (ContactEntry)((IStructuredSelection)event.getSelection()).getFirstElement();				
				setPageComplete(!event.getSelection().isEmpty());
			}			
		});
		
		//viewer.setInput(getViewSite());
	}
	
	// You need to overwrite this method otherwise you receive a
	// AssertionFailedException
	// This method should always return the top widget of the application
	@Override
	public Control getControl() {
		return container;
	}

	
}
